getUserInfo();
// 获取用户信息
function getUserInfo(){
    $.ajax({
        type: "get",
        url: basePath + '/admin/users/query/'+auth.username,
        dataType: "json",
        //这里是重点
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("token", auth.token);
        },
        success: function (data) {
            if(data.code=="00000"){
                let user=data.data;
                var roleIds=[];
                let roles=user.roles;
                for(var k=0;k<roles.length;k++){
                    roleIds.push(roles[k].roleId);
                }
                roleIds = roleIds.join(",");
                getMenus(roleIds);
                $("#userName").html(user.nickname);
                $("#headPic").attr("src",user.avatar);
            }
        },
        error:function(error){console.log(error);}
    });
};

// 获取菜单
function getMenus(roleId){
    $.ajax({
        type: "get",
        url: basePath + '/admin/menus/Index?roleIds='+roleId,
        async: false,
        dataType: "json",
        //这里是重点
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("token", auth.token);
        },
        success: function (res) {
            if(res.code=="00000"){
                let menus=res.data;
                let temp = [];
                setMenus(menus,0,temp);
                $("#menusList").html(temp.join(''));
            }
        },
        error:function(error){console.log(error);}
    });
};

// 设置菜单
var setMenus = function (menu, root,temp) {
    for (var i = 0; i < menu.length; i++) {
        if(root==0){
            temp.push("<li class='layui-nav-item'>");
            temp.push("<a><i class='"+menu[i].icon+"'></i>&emsp;<cite>"+menu[i].label+"</cite></a>");
            if(menu[i].children.length>0){
                temp.push("<dl class='layui-nav-child'>");
            }
        }

        if (root==menu[i].parentId) {
            if(menu[i].parentId!=0){
                if(menu[i].type==0){
                    if(menu[i].children.length>0) {
                        temp.push("<dd><a>"+menu[i].label+"</a><dl class='layui-nav-child'>");
                    }
                }else{
                    temp.push("<dd><a lay-href='"+menu[i].path+"'>"+menu[i].label+"</a></dd>");
                }
            }

            setMenus(menu[i].children, menu[i].id,temp);

            if(menu[i].parentId!=0) {
                if (menu[i].type == 0) {
                    if (menu[i].children.length > 0) {
                        temp.push("</dl></dd>");
                    }
                }
            }
        }

        if(root==0){
            if(menu[i].children.length>0){
                temp.push("</dl>");
            }
            temp.push("</li>");
        }
    }
};


